﻿/*
   Copyright 2011 Softisis

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FOM.Impl;
using FOM.Impl.Converters;
using FOM.Interfaces;
using NUnit.Framework;

namespace TestUnits
{
    /// <summary>
    /// Goto is a link to a source/target pair, without maping '(i.e. source is not copied
    /// into instance).
    /// F.Ex
    /// </summary>
    /// <remarks>Copyright Softisis 2011 (France)</remarks>
    [TestFixture]
    public class GotoMappingsTests
    {


        [SetUp]
        public void SetUp()
        {
        }

        /// <summary>
        /// Test the GoTo, with a null object on the source path.
        /// </summary>
        [Test]
        public void TestSimpleGotoOnEmptyTargetInPath()
        {
            // create manager
            IManager mngr = new ManagerImpl();

            var rootmap = mngr.RegisterMapping<ObjectA, ObjectA>("test");
            rootmap.GoTo(a1 => a1.UnObjetB.BUnobjectC, a2 => a2.UnObjetB.BUnobjectC)
                .Map(c1 => c1.Val1, c2 => c2.Val1);
            var source = TestObjectsHolder.GetObjectA();

            var res = (ObjectA)mngr.ApplyMapping("test", source);
            Assert.IsTrue(res != null);
            Assert.IsTrue(res.UnObjetB == null);
        }

        [Test]
        public void TestGotoWithNoMoveTargetWithReverse()
        {
            // create manager
            IManager mngr = new ManagerImpl();

            var rootmap = mngr.RegisterMapping<ObjectB, ObjectC>("test");
            rootmap.Map(b => b.BEntier, c => c.Val1);
            rootmap.Map(b => b.BUnEnum, c => c.CChaine).UseConverter(new ValueTypeToStringConv()); // enum converter
            // goto 
            var nextnode = rootmap.GoTo(b => b, c => c.ob);
            nextnode.Map(bs => bs.BCar, bd => bd.BCar);

            var invert = rootmap.Reverse();
            mngr.RegisterMapping(invert, "invtest");

            var source = TestObjectsHolder.GetObjectC();
            // set an enum string value, to check that reversing converter will work
            source.CChaine = "" + MonEnum.UnDeuxiemeEnum;

            var res = (ObjectB)mngr.ApplyMapping("invtest", source);

            Assert.IsTrue(res != null);
            Assert.IsTrue(source.Val1.Equals(res.BEntier));
            Assert.IsTrue(source.ob.BCar.Equals(res.BCar));
            Assert.IsTrue(res.BUnEnum.Equals(MonEnum.UnDeuxiemeEnum));
        }

        [Test]
        public void TestGotoWithNoMoveTarget2()
        {
            // create manager
            IManager mngr = new ManagerImpl();

            var rootmap = mngr.RegisterMapping<ObjectC, ObjectB>("test");
            rootmap.Map(c => c.Val1, b => b.BEntier);
            // goto 
            var nextnode = rootmap.GoTo(c => c.ob, b => b);
            nextnode.Map(bs => bs.BCar, bd => bd.BCar);

            var source = TestObjectsHolder.GetObjectC();
            var res = (ObjectB)mngr.ApplyMapping("test", source);


            Assert.IsTrue(res != null);
            Assert.IsTrue(source.Val1.Equals(res.BEntier));
            Assert.IsTrue(source.ob.BCar.Equals(res.BCar));
        }




        /// <summary>
        /// Test one GoTo. Goto is a link to a source/target pair, without maping '(i.e. source is not copied
        /// into instance) but mapping cursor is set to the source and target props
        /// </summary>
        [Test]
        public void TestInvalidOperationOnGoto()
        {
            // create manager
            IManager mngr = new ManagerImpl();

            var rootmap = mngr.RegisterMapping<ObjectA, ObjectA>("test");

            bool ok = false;
            try
            {
                rootmap.GoTo(a1 => a1.UnObjetB.BUnobjectC, a2 => a2.UnObjetB.BUnobjectC).NewInstance();
                ok = true;
            }
            catch
            {
            }

            try
            {
                rootmap.GoTo(a1 => a1.UnObjetB.BUnobjectC, a2 => a2.UnObjetB.BUnobjectC).UseConverter(null);
                ok = true;
            }
            catch
            {
            }

            Assert.IsFalse(ok==true);
        }


        /// <summary>
        /// Test the GoTo, with a null object on the source path.
        /// </summary>
        [Test]
        public void TestMultipleGoto()
        {
            // create manager
            IManager mngr = new ManagerImpl();

            var rootmap = mngr.RegisterMapping<ObjectA, ObjectA>("test");
            rootmap.GoTo(a1 => a1, a2 => a2)
                .GoTo(a => a.UnObjetB, aa => aa.UnObjetB)
                .GoTo(b => b.BUnobjectC, bb => bb.BUnobjectC)
                .Map(c1 => c1.Val1, c2 => c2.Val1);

            var source = TestObjectsHolder.GetObjectA();
            source.UnObjetB.BUnobjectC=new ObjectC();
            source.UnObjetB.BUnobjectC.Val1 = 456123;

            var res = (ObjectA)mngr.ApplyMapping("test", source);
            Assert.IsTrue(res!=null);
            Assert.IsTrue(res.UnObjetB != null);
            Assert.IsTrue(res.UnObjetB.BUnobjectC != null);
            Assert.IsTrue(res.UnObjetB.BUnobjectC.Val1 == 456123);

            // valid that goto did not map anything
            Assert.IsTrue(res.Car == '\0');
            Assert.IsTrue(res.UnObjetB.BEntier == 0);
            Assert.IsTrue(res.UnObjetB.BUnobjectC.ob== null);
        }


        /// <summary>
        /// Test the GoTo to an object, an map to the object itelsef, and add some other mappings
        /// </summary>
        [Test]
        public void TestGotoAndObjMap()
        {
            // create manager
            IManager mngr = new ManagerImpl();

            var rootmap = mngr.RegisterMapping<ObjectA, ObjectA>("test");
            rootmap.GoTo(a1 => a1.UnObjetB, a2 => a2.UnObjetB.BUnObjetA.UnObjetB)
                .Map(b1 => b1, b2 => b2); // won't do anything interesting in fact

            rootmap.Map(a1 => a1.UnObjetB.BChaine, a2 => a2.UnObjetB.BUnObjetA.UnObjetB.BChaine);
            rootmap.Map(a1 => a1.Anid, a2 => a2.UnObjetB.BUnObjetA.UnObjetB.BEntier);

            var source = TestObjectsHolder.GetObjectA();
            var target = new ObjectA();
            target.UnObjetB=new ObjectB();
            target.UnObjetB.BUnObjetA=new ObjectA();
            target.UnObjetB.BUnObjetA.UnObjetB = new ObjectB();
            target.UnObjetB.BUnObjetA.UnObjetB.BEntier = 987654; // will be replaced by mapping
            target.UnObjetB.BUnObjetA.UnObjetB.BDoubleobject = 10.23; // will be kept
 
            var res = (ObjectA)mngr.ApplyMapping("test", source, target);
            Assert.IsTrue(res != null);
            Assert.IsTrue(res.UnObjetB != null);
            Assert.IsTrue(res.UnObjetB.BUnObjetA != null);
            Assert.IsTrue(res.UnObjetB.BUnObjetA.UnObjetB != null);

            // valid that goto did not map anything
            Assert.IsTrue(res.Car == '\0');
            Assert.IsTrue(res.UnObjetB.BEntier == 0);
            Assert.IsTrue(res.UnObjetB.BUnobjectC == null);

            // valid that the mapping are ok
            Assert.IsTrue(res.UnObjetB.BUnObjetA.UnObjetB.BEntier == source.Anid); // old value replaced
            Assert.IsTrue(target.UnObjetB.BUnObjetA.UnObjetB.BDoubleobject == 10.23);
            Assert.IsTrue(target.UnObjetB.BUnObjetA.UnObjetB.BChaine == source.UnObjetB.BChaine);
        }
    }
}
